package ma.ensao.evalmetrics.controller.metrics;

//
//import java.io.BufferedReader;
//import java.io.File;
//import java.io.FileInputStream;                                       
//import java.io.FileReader;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.InputStreamReader;
//import java.io.StreamTokenizer;
//import java.util.List;
//
//import ma.ensao.evalmetrics.model.CLOC;
//import ma.ensao.evalmetrics.model.NOC;
//
//import com.opensymphony.xwork2.ActionSupport;
//
//public class NOCManager {
//	private NOC noc=new NOC();
//	int cmt=0;
//public  NOCManager() {
//		
//	}
//public NOC getNoc() {
//	return noc;
//}
//public void setNoc(NOC noc) {
//	this.noc = noc;
//}
//public int getCmt() {
//	return cmt;
//}
//public void setCmt(int cmt) {
//	this.cmt = cmt;
//}
//public NOC read(File file) throws IOException {
//	
//		InputStream ips = new FileInputStream(file);
//		InputStreamReader ipsr = new InputStreamReader(ips);
//		BufferedReader br = new BufferedReader(ipsr);
//		String ligne;
//		 int cmt=0;
//		while ((ligne = br.readLine()) != null) {
////			ligne.trim();
////			if(ligne.contains("extends")==true){
////				cmt++;
////			}
//			if(ligne.contains("class")== true){
//				 System.out.println(file.getClass().getName());
//			}
//				
//		}
//		System.out.println("nocbre de commentaires est:"+""+cmt);
//		
//		br.close();
//		noc.setCmt(cmt);
//		return noc;
//	}
//
//}

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.BrokenBarrierException;

import javassist.compiler.Javac;
import javassist.expr.NewArray;

import javax.swing.plaf.metal.MetalIconFactory.FolderIcon16;

import com.thoughtworks.qdox.JavaDocBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaSource;

import ma.ensao.evalmetrics.model.NOC;


public class NOCManager {
	NOC noc =new NOC();
	String NameClasse;
	
	File FileP;
	JavaDocBuilder builder = new JavaDocBuilder();

	

	private void filtre(File file, JavaDocBuilder builder) {
		File[] files = file.listFiles();
		System.out.println(files.length);
		for (int c = 0; c < files.length; c++) {
			if (files[c].isFile()) {
				if (files[c].getName().endsWith(".java")) {
					try {
						builder.addSource(files[c]);
						// w++;
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			} else if (files[c].isDirectory()) {
				String path = files[c].getAbsolutePath().replace("\\", "/");
				String vPath = path.concat("/" + files[c].getName());
				System.out.println("Le path es        " + path);
				File sfile = new File(path);
				filtre(sfile, builder);
			}
		}
	}

	public NOCManager() {
	
	}
	public NOC read(File file) throws FileNotFoundException, IOException{
		JavaDocBuilder b=new JavaDocBuilder();
		b.addSource(file);
		JavaSource sr=b.getSources()[0];
		JavaClass jc =sr.getClasses()[0];
		int cmt=0;
		JavaDocBuilder builder =new JavaDocBuilder();
		File FileP =file;
		
		while(!FileP.getName().equals("src")){
			FileP=FileP.getParentFile();
		}
		System.out.println(FileP.getName());
		filtre(FileP, builder);
		for(int i=0;i<builder.getSources().length;i++){
			System.out.println(builder.getSources()[i].getClasses()[0].getName());
		}
		for(JavaSource s : builder.getSources()){
			JavaClass j=s.getClasses()[0];
			//System.out.println(jc.getName());
			//System.out.println("fffff"+j.getSuperJavaClass().getName());
			System.out.println(jc.getPackageName());
			//System.out.println(j.getSuperJavaClass().getPackageName());
			if(j.getSuperJavaClass()!=null){
				if(jc.getName().equals(j.getSuperJavaClass().getName())&&(jc.getPackageName().equals(j.getSuperJavaClass().getPackageName()))){
					System.out.println("=");
					cmt++;
					System.out.println("cc"+cmt);
				}
			}
			
			
		}
		noc.setCmt(cmt);
		System.out.println(cmt);
		return noc;
	}
	public static void main(String[] args) throws IOException {
		File file = new File("C:/work/Algorithme/src/ma/ensa/test/test.java");

		NOCManager a = new NOCManager();
		a.read(file);
	}
}
